////////////////////////////////////////////////
// -- DepthHUD Lite                           //
// by Hurricaaane (Ha3)                       //
//                                            //
// http://www.youtube.com/user/Hurricaaane    //
//--------------------------------------------//
// Menu                                       //
////////////////////////////////////////////////

local DHLITE_LOCAL_MENU_WIDTH = 300

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//// DERMA PANEL .


function dhlite.Util_FrameGetExpandTable( myPanel )
	local expandTable = {}
	
	for k,subtable in pairs( myPanel.Categories ) do
		table.insert(expandTable, subtable[1]:GetExpanded())
		
	end
	
	return expandTable
end

function dhlite.Util_AppendPanel( myPanel, thisPanel )
	local toAppendIn = myPanel.Categories[#myPanel.Categories][1].List
	
	thisPanel:SetParent( toAppendIn )
	toAppendIn:AddItem( thisPanel )
	
end

function dhlite.Util_AppendCheckBox( myPanel, title, cvar )

	local checkbox = vgui.Create( "DCheckBoxLabel" )
	checkbox:SetText( title )
	checkbox:SetConVar( cvar )
	
	dhlite.Util_AppendPanel( myPanel, checkbox )
	
end

function dhlite.Util_AppendLabel( myPanel, sText, optiSize, optbWrap )

	local label = vgui.Create( "DLabel" )
	label:SetText( sText )
	
	--local useSY = false
	
	if optiSize then
		label:SetWrap( true )
		label:SetContentAlignment( 2 )
		if (type(optiSize) == "number") then
			label:SetSize( myPanel.W_WIDTH, optiSize )
		
		else
			--useSY = true
			label:SetWide( myPanel.W_WIDTH )
			label:SetAutoStretchVertical( true )
			--label:PerformLayout( )
			--label:SizeToContentsY( )
			--label:SizeToContents()
			
		end
		
	end
	
	if optbWrap then
		label:SetWrap( true )
		
	end
	
	dhlite.Util_AppendPanel( myPanel, label )
	--if useSY then label:PerformLayout() label:SizeToContents() end
	
end

function dhlite.Util_AppendSlider( myPanel, sText, sCvar, fMin, fMax, iDecimals)
	local slider = vgui.Create("DNumSlider")
	slider:SetText( sText )
	slider:SetMin( fMin )
	slider:SetMax( fMax )
	slider:SetDecimals( iDecimals )
	slider:SetConVar( sCvar )
	
	dhlite.Util_AppendPanel( myPanel, slider )
end

function dhlite.Util_MakeFrame( width, height, optsTitle )
	local myPanel = vgui.Create( "DFrame" )
	local border = 4
	
	myPanel.W_HEIGHT = height - 20
	myPanel.W_WIDTH = width - 2 * border
	
	myPanel:SetPos( ScrW() * 0.5 - width * 0.5 , ScrH() * 0.5 - height * 0.5 )
	myPanel:SetSize( width, height )
	myPanel:SetTitle( DHLITE_NAME )
	myPanel:SetVisible( false )
	myPanel:SetDraggable( true )
	myPanel:ShowCloseButton( true )
	myPanel:SetDeleteOnClose( false )
	
	myPanel.Contents = vgui.Create( "DPanelList", myPanel )
	myPanel.Contents:SetPos( border , 22 + border )
	myPanel.Contents:SetSize( myPanel.W_WIDTH, height - 2 * border - 22 )
	myPanel.Contents:SetSpacing( 5 )
	myPanel.Contents:EnableHorizontal( false )
	myPanel.Contents:EnableVerticalScrollbar( false )
	
	myPanel.Categories = {}
	
	return myPanel
end

function dhlite.Util_MakeCategory( myPanel, sTitle, bExpandDefault )
	local category = vgui.Create("DCollapsibleCategory", myPanel.Contents)
	category.List  = vgui.Create("DPanelList", category )
	table.insert( myPanel.Categories, {category, bExpandDefault} )
	category:SetSize( myPanel.W_WIDTH, 50 )
	category:SetLabel( sTitle )
	
	category.List:SetPadding( 2 )
	category.List:SetSpacing( 2 )
	
	category.List:EnableHorizontal( false )
	category.List:EnableVerticalScrollbar( false )
	
	return category
end

function dhlite.Util_ApplyCategories( myPanel, opt_tExpand )
	for k,subtable in pairs( myPanel.Categories ) do
		subtable[1]:SetExpanded( opt_tExpand and (opt_tExpand[k] and 1 or 0) or subtable[2] )
		subtable[1].List:SetSize( myPanel.W_WIDTH, myPanel.W_HEIGHT - #myPanel.Categories * 10 - 10 )
		subtable[1]:SetSize( myPanel.W_WIDTH, myPanel.W_HEIGHT - #myPanel.Categories * 10 )
		
		subtable[1].List:PerformLayout()
		subtable[1].List:SizeToContents()
		
		subtable[1]:SetContents( subtable[1].List )
		
		myPanel.Contents:AddItem( subtable[1] )
	end
	
end

function dhlite.MenuCall_ReloadFromCloud()
	if dhlite_cloud then
		dhlite_cloud:Ask()
	end
	
end

function dhlite.MenuCall_ReloadFromLocale()
	if dhlite_cloud then
		dhlite_cloud:LoadLocale()
	end
	
end

function dhlite.BuildMenu( opt_tExpand, opt2_iXpos, opt2_iYpos )
	if dhlite.DermaPanel then dhlite.DermaPanel:Remove() end
	
	local bCanGetVersion = dhlite_internal ~= nil
	local MY_VERSION, ONLINE_VERSION, DOWNLOAD_LINK
	if bCanGetVersion then
		MY_VERSION, ONLINE_VERSION, DOWNLOAD_LINK = dhlite_internal.GetVersionData()
		
		if ONLINE_VERSION == -1 then
			ONLINE_VERSION = "<offline>"
		end
		
	end
	
	dhlite.DermaPanel = dhlite.Util_MakeFrame( DHLITE_LOCAL_MENU_WIDTH, ScrH() * 0.7 )
	local refPanel = dhlite.DermaPanel
	
	dhlite.Util_MakeCategory( refPanel, "General", (true) and 1 or 0 )
	dhlite.Util_AppendCheckBox( refPanel, "Enable" , "dhlite_core_enable" )
	dhlite.Util_AppendCheckBox( refPanel, "Watch development progress" , "dhlite_core_debug" )
	dhlite.Util_AppendLabel( refPanel, "The HUD is not done yet and is not yet customizable. If you want to watch development progress, check this box and you'll be able to see the unfinished HUD. NOTE : Having DHL disabled while Watch mode is enabled may sill run DHI code in background !", 120, true )
	
	--Helper label
	do
		local GeneralTextLabelMessage = "The command \"dhlite_menu\" calls this menu.\n"
		GeneralTextLabelMessage = GeneralTextLabelMessage .. "Example : To assign " .. DHLITE_NAME .. " menu to F7, type in the console :"
		dhlite.Util_AppendLabel( refPanel, GeneralTextLabelMessage, 40, true )
		
	end
	--Helper multiline
	do
		local GeneralCommandLabel = vgui.Create("DTextEntry")
		GeneralCommandLabel:SetText( "bind \"F7\" \"dhlite_menu\"" )
		GeneralCommandLabel:SetEditable( false )

		dhlite.Util_AppendPanel( refPanel, GeneralCommandLabel )
		
	end
	dhlite.Util_AppendLabel( refPanel, "You can quickly call the menu by binding \"+dhlite_menu\" to one of your keys : The menu closes when you release the key.", 40, true )
	
	dhlite.Util_MakeCategory( refPanel, "Cloud" .. (bCanGetVersion and (" [ v" .. tostring(MY_VERSION) .. " >> v" .. tostring(ONLINE_VERSION) .. " ]") or " Version" ), 0 )
	-- Reload from Cloud Button
	do
		local CReload = vgui.Create("DButton")
		CReload:SetText( "Reload from Cloud" )
		CReload.DoClick = dhlite.MenuCall_ReloadFromCloud
		dhlite.Util_AppendPanel( refPanel, CReload )
	end
	
	-- Reload from Locale Button
	if dhlite_internal then
		local CReload = vgui.Create("DButton")
		CReload:SetText( "Reload from Locale" )
		CReload.DoClick = dhlite.MenuCall_ReloadFromLocale
		dhlite.Util_AppendPanel( refPanel, CReload )
	end

	dhlite.Util_AppendLabel( refPanel, "If the Cloud ever crashes yet you still have an Internet connection, you can still request the Cloud by typing \"dhlite_cloud_ask\" in the console. If it still doesn't work, then Google Code is on maintenance or your Network connection is flawed.", 80, true )
	
	dhlite.Util_ApplyCategories( refPanel, opt_tExpand )
	
	if opt2_iXpos and opt2_iYpos then
		refPanel:SetPos( opt2_iXpos, opt2_iYpos )
	end
end

function dhlite.ShowMenu()
	if not dhlite.DermaPanel then
		dhlite.BuildMenu()
	end
	--dhlite.DermaPanel:Center()
	dhlite.DermaPanel:MakePopup()
	dhlite.DermaPanel:SetKeyboardInputEnabled( false )
	dhlite.DermaPanel:SetVisible( true )
end

function dhlite.HideMenu()
	if not dhlite.DermaPanel then
		return
	end
	dhlite.DermaPanel:SetVisible( false )
end

function dhlite.DestroyMenu()
	if dhlite.DermaPanel then
		dhlite.DermaPanel:Remove()
		dhlite.DermaPanel = nil
	end
end

--[[
function dhlite.MakePresetPanel( data )
	local ctrl = vgui.Create( "ControlPresets", self )
	
	ctrl:SetPreset( data.folder )
	
	if ( data.options ) then
		for k, v in pairs( data.options ) do
			if ( k != "id" ) then
				ctrl:AddOption( k, v )
			end
		end
	end
	
	if ( data.cvars ) then
		for k, v in pairs( data.cvars ) do
			ctrl:AddConVar( v )
		end
	end
	
	return ctrl
end
]]--

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//// SANDBOX PANEL .

function dhlite.Panel(Panel)	
	Panel:AddControl("Checkbox", {
			Label = "Enable Auto-mount", 
			Description = "Enable Auto-mount", 
			Command = "dhlite_core_enable" 
		}
	)
	Panel:AddControl("Button", {
			Label = "Open Menu (dhlite_menu)", 
			Description = "Open Menu (dhlite_menu)", 
			Command = "dhlite_menu"
		}
	)
	
	Panel:Help("To trigger the menu in any gamemode, type dhlite_menu in the console, or bind this command to any key.")
end

function dhlite.AddPanel()
	spawnmenu.AddToolMenuOption("Options", "Player", DHLITE_NAME, DHLITE_NAME, "", "", dhlite.Panel, {SwitchConVar = 'dhlite_core_enable'})
end

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// MOUNT FCTS.

function dhlite.MountMenu()
	concommand.Add( "dhlite_menu", dhlite.ShowMenu )
	concommand.Add( "dhlite_call_menu", dhlite.ShowMenu )
	concommand.Add( "+dhlite_menu", dhlite.ShowMenu )
	concommand.Add( "-dhlite_menu", dhlite.HideMenu )
	hook.Add( "PopulateToolMenu", "AddDepthHUD LitePanel", dhlite.AddPanel )
end

function dhlite.UnmountMenu()
	dhlite.DestroyMenu()

	concommand.Remove( "dhlite_call_menu" )
	concommand.Remove( "dhlite_menu" )
	concommand.Remove( "+dhlite_menu" )
	concommand.Remove( "-dhlite_menu" )
	hook.Remove( "PopulateToolMenu", "AddDepthHUD LitePanel" )
end

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////